package com.game.server.db.entity;

import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.dao.XyClubChangesQuotaDao;
import com.game.server.db.dao.XyClubContributionLogDao;
import com.game.server.db.dao.XyClubMemberInfoDao;
import com.game.server.db.dao.XyClubPremiumOplogDao;
import com.game.server.message.proto.out.TabOutintegralChangeRsp;
import com.game.server.util.CompuUtils;
import lombok.Getter;
import lombok.Setter;

import java.math.BigDecimal;
import java.sql.Timestamp;

@Getter
@Setter
public class XyClubMemberInfo {

    public long id; //俱乐部用户唯一ID
    public int uid; //用户ID
    public int cid; // 俱乐部ID
    public int isManager; //在俱乐部的身份  1创建者 2.管理员 3.普通成员
    public int xingyuvalue; //拥有信誉额度
    public int gongxianbili;//贡献比例
    public int isAccessory; //是否为合伙人0 不是 1是
    public int pid; //上级合伙人ID
    public int partner_id; //成为合伙人时的ID
    public int del_type; //账号状态 0正常 1已删除
    public int balance_bonus; //剩余红利
    public long aggregate_bonus; //累计红利
    public int bonus_share;//红利分配比例
    public Timestamp createdate; //加入俱乐部时间



    public void NoticeTabUser(int val){
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(this.uid);
        if (client!=null && client.getClubid()>0 && client.getTableid()>0){
            client.getVo().NoticeTabUserXinyuChange(client,2,val);
        }
    }

    /**
     * 俱乐部用户结算
     *
     * @param type   1增加、2减少、3带入、4结算 5贡献 6带入退回
     * @param roomid 房间号
     * @param meUid  操作者ID type 1 2 需要
     * @param dragIn 玩家总带入 type 4 需要
     * @return 0 成功 1失败 2余额不足
     */
    public synchronized int updateXingyuvalue(int type, int value, int roomid, int meUid, int dragIn,long roomkey) {
        XyClubChangesQuota insertInto = new XyClubChangesQuota();
        if (type == 1) {
            int upCount = XyClubMemberInfoDao.addXingyuvalue(this.id, value);
            if (upCount > 0) {
                this.setXingyuvalue(this.xingyuvalue + value);
                insertInto.setCid(this.cid);
                insertInto.setMe_uid(meUid);
                insertInto.setTo_uid(this.uid);
                insertInto.setType(1);
                insertInto.setQuota(value);
                insertInto.setResidue(this.xingyuvalue);
                insertInto.setRoomkey(0);
                XyClubChangesQuotaDao.insertInto(insertInto);

                NoticeTabUser(value);
                return 0;
            }
        }
        if (type == 2) {
            if (this.xingyuvalue >= value) {
                int upCount = XyClubMemberInfoDao.addXingyuvalue(this.id, -value);
                if (upCount > 0) {
                    this.setXingyuvalue(this.xingyuvalue - value);
                    insertInto.setCid(this.cid);
                    insertInto.setMe_uid(meUid);
                    insertInto.setTo_uid(this.uid);
                    insertInto.setType(2);
                    insertInto.setQuota(-value);
                    insertInto.setResidue(this.xingyuvalue);
                    insertInto.setRoomkey(0);
                    XyClubChangesQuotaDao.insertInto(insertInto);

                    NoticeTabUser(-value);
                    return 0;
                }
            }else{
                return 2;
            }
        }
        if (type == 3) {
            if (this.xingyuvalue >= value) {
                int upCount = XyClubMemberInfoDao.addXingyuvalue(this.id, -value);
                if (upCount > 0) {
                    this.setXingyuvalue(this.xingyuvalue - value);
                    insertInto.setCid(this.cid);
                    insertInto.setMe_uid(roomid);
                    insertInto.setTo_uid(this.uid);
                    insertInto.setType(3);
                    insertInto.setQuota(-value);
                    insertInto.setResidue(this.xingyuvalue);
                    insertInto.setRoomkey(roomkey);
                    XyClubChangesQuotaDao.insertInto(insertInto);
                    return 0;
                }
            }else{
                return 2;
            }
        }
        if (type == 4) {
            int upCount = XyClubMemberInfoDao.addXingyuvalue(this.id, value);
            if (upCount > 0) {
                this.setXingyuvalue(this.xingyuvalue + value);
                insertInto.setCid(this.cid);
                insertInto.setMe_uid(roomid);
                insertInto.setTo_uid(this.uid);
                insertInto.setType(4);
                insertInto.setQuota(value);
                insertInto.setResidue(this.xingyuvalue);
                insertInto.setRoomkey(roomkey);
                XyClubChangesQuotaDao.insertInto(insertInto);
                return 0;
            }
        }
        if (type == 5) {
            int upCount = XyClubMemberInfoDao.addXingyuvalue(this.id, value);
            if (upCount > 0) {
                this.setXingyuvalue(this.xingyuvalue + value);
                insertInto.setCid(this.cid);
                insertInto.setMe_uid(roomid);
                insertInto.setTo_uid(this.uid);
                insertInto.setType(5);
                insertInto.setQuota(value);
                insertInto.setResidue(this.xingyuvalue);
                insertInto.setRoomkey(roomkey);
                insertInto.setPid(meUid);
                XyClubChangesQuotaDao.insertInto(insertInto);
                return 0;
            }
        }
        if (type == 6) {
            int upCount = XyClubMemberInfoDao.addXingyuvalue(this.id, value);
            if (upCount > 0) {
                this.setXingyuvalue(this.xingyuvalue + value);
                insertInto.setCid(this.cid);
                insertInto.setMe_uid(roomid);
                insertInto.setTo_uid(this.uid);
                insertInto.setType(3);
                insertInto.setQuota(value);
                insertInto.setResidue(this.xingyuvalue);
                insertInto.setRoomkey(roomkey);
                XyClubChangesQuotaDao.insertInto(insertInto);
                return 0;
            }
        }
        return 1;
    }

    /**
     * 修改合伙人剩余红利
     *
     * @param type        操作类型 1 增加 2减少
     * @param value       type == 2:操作数量  type==1:玩家最后的输赢分数
     * @param pid         type ==2:操作人的用户ID  type==1 贡献人ID
     * @param relevanceId type==1 贡献人ID  只有为增加类型时使用
     * @return isok 0 成功 1失败
     */
    public synchronized int updateBalanceBonus(int value, int type, int pid, int relevanceId) {
        int isok = 1;
        if (type == 1) {
            if (value > 0 ) {
                int updateCount = XyClubMemberInfoDao.updateBalanceBonus(this.id, value);
                if (updateCount > 0) {
                    this.setBalance_bonus(this.balance_bonus + value);
                    isok = 0;
                    this.addAggregateBonus(value);//增加累计
//                    XyClubPremiumOplog insertInto = new XyClubPremiumOplog();//写入记录
//                    insertInto.setCid(this.cid);
//                    insertInto.setPid(pid);
//                    insertInto.setUid(this.uid);
//                    insertInto.setVal(value);
//                    insertInto.setResidue_val(this.getBalance_bonus());
//                    XyClubPremiumOplogDao.insertInto(insertInto);
                }
            }
        }
        if (type == 2) {
            if (value > 0 && this.balance_bonus >= value) {
                int updateCount = XyClubMemberInfoDao.updateBalanceBonus(this.id, -value);
                if (updateCount > 0) {
                    this.setBalance_bonus(this.balance_bonus - value);
                    isok = 0;
                    //this.addAggregateBonus(value);
                    //增加累计
                    XyClubPremiumOplog insertInto = new XyClubPremiumOplog();//写入记录
                    insertInto.setCid(this.cid);
                    insertInto.setPid(pid);
                    insertInto.setUid(this.uid);
                    insertInto.setVal(-value);
                    insertInto.setResidue_val(this.getBalance_bonus());
                    XyClubPremiumOplogDao.insertInto(insertInto);
                }
            }else{
                isok = 2;
            }
        }
        return isok;
    }

    /**
     * 增加合伙人累计红利
     *
     * @param value
     */
    public synchronized void addAggregateBonus(int value) {
        if (value > 0) {
            XyClubMemberInfoDao.addAggregateBonus(this.id, value);
            this.setAggregate_bonus(this.aggregate_bonus + Long.valueOf(value));
        }
    }
}
